function CIScrollSpy() {
    this.el = null;
    this.index = 0;
    this.container = null;
    this.spyY = 500;
    this.checkPoints = [];
    this.events = {};
}

CIScrollSpy.prototype.init = function(el, opts) {
    var self = this;
    this.el = el;
    this.container = opts.container || window;
    this.spyY = (opts.spyY >= 0) ? opts.spyY : this.container.innerHeight;
    this.checkPoints = opts.checkPoints || this.checkPoints;

    this.container.scrollTo(0, 0);
    this.fire('init', {
        index: this.index,
        scrollY: this.container.scrollY
    });
    this.bindEvent();
};

CIScrollSpy.prototype.bindEvent = function() {
    var self = this;
    self.container.addEventListener('touchmove', function (evt) {
        var scrollY = self.container.scrollY;
        self.fire('scroll', {
            index: self.index,
            scrollY: scrollY
        });
        if (scrollY >= self.checkPoints[self.index] - self.spyY) {
            // console.log('enter', self.index, self.spyY, scrollY);
            self.fire('enter', {
                index: self.index,
                scrollY: scrollY
            });
            self.index++;
        }
        if (scrollY < self.checkPoints[self.index - 1] - self.spyY) {
            self.index--;
            // console.log('leave', self.index, self.spyY, scrollY);
            self.fire('leave', {
                index: self.index,
                scrollY: scrollY
            });
        }
    });
};

CIScrollSpy.prototype.on = function(type, handller) {
    if (!this.events[type]) {
        this.events[type] = [];
    }
    this.events[type].push(handller);
};

CIScrollSpy.prototype.fire = function(type, obj) {
    if (this.events[type]) {
        var events = this.events[type];
        for (var i = 0; i < events.length; i++) {
            if ('function' == typeof events[i]) {
                events[i](obj);
            }
        }
    }
};
